package com.ejlchina.common.util;

public class StringUtils {

	
	/**
	 * 首字母变小写
	 */
	public static String firstCharToLowerCase(String str) {
		char firstChar = str.charAt(0);
		if (firstChar >= 'A' && firstChar <= 'Z') {
			char[] arr = str.toCharArray();
			arr[0] += ('a' - 'A');
			return new String(arr);
		}
		return str;
	}
	
	/**
	 * 首字母变大写
	 */
	public static String firstCharToUpperCase(String str) {
		char firstChar = str.charAt(0);
		if (firstChar >= 'a' && firstChar <= 'z') {
			char[] arr = str.toCharArray();
			arr[0] -= ('a' - 'A');
			return new String(arr);
		}
		return str;
	}
	
	/**
	 * 字符串为 null 或者内部字符全部为 ' ' '\t' '\n' '\r' 这四类字符时返回 true
	 */
	public static boolean isBlank(String str) {
		if (str == null) {
			return true;
		}
		int len = str.length();
		if (len == 0) {
			return true;
		}
		for (int i = 0; i < len; i++) {
			switch (str.charAt(i)) {
			case ' ':
			case '\t':
			case '\n':
			case '\r':
			// case '\b':
			// case '\f':
				break;
			default:
				return false;
			}
		}
		return true;
	}
	
	public static boolean notBlank(String str) {
		return !isBlank(str);
	}
	
	public static boolean notBlank(String... strings) {
		if (strings == null) {
			return false;
		}
		for (String str : strings) {
			if (isBlank(str)) {
				return false;
			}
		}
		return true;
	}
	
	public static boolean notNull(Object... paras) {
		if (paras == null) {
			return false;
		}
		for (Object obj : paras) {
			if (obj == null) {
				return false;
			}
		}
		return true;
	}
	
	public static String toCamelCase(String stringWithUnderline) {
		if (stringWithUnderline.indexOf('_') == -1) {
			return stringWithUnderline;
		}
		
		stringWithUnderline = stringWithUnderline.toLowerCase();
		char[] fromArray = stringWithUnderline.toCharArray();
		char[] toArray = new char[fromArray.length];
		int j = 0;
		for (int i=0; i<fromArray.length; i++) {
			if (fromArray[i] == '_') {
				// 当前字符为下划线时，将指针后移一位，将紧随下划线后面一个字符转成大写并存放
				i++;
				if (i < fromArray.length) {
					toArray[j++] = Character.toUpperCase(fromArray[i]);
				}
			}
			else {
				toArray[j++] = fromArray[i];
			}
		}
		return new String(toArray, 0, j);
	}
	
	public static String join(String[] stringArray) {
		StringBuilder sb = new StringBuilder();
		for (String s : stringArray) {
			sb.append(s);
		}
		return sb.toString();
	}

	
	public static String getRandomUUID() {
		return java.util.UUID.randomUUID().toString().replace("-", "");
	}
	
	
    public static String join(String[] strAry, String separator){
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < strAry.length; i++){
            if(i == (strAry.length-1)) {
                sb.append(strAry[i]);
            } else{
                sb.append(strAry[i]).append(separator);
            }
        }
        return new String(sb);
    }
    
    
    public static String join(long[] array, String separator){
    	Object[] arr = new Object[array.length];
    	for (int i = 0; i < array.length; i++) {
    		arr[i] = array[i];
    	}
    	return join(arr, separator);
    }
    
    
    public static String join(Object[] array, String separator){
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < array.length; i++){
            if(i == (array.length-1)) {
                sb.append(array[i]);
            } else{
                sb.append(array[i]).append(separator);
            }
        }
        return new String(sb);
    }
	
    public static String getLastByToken(String src, String token) {
    	if (src == null) {
    		throw new IllegalArgumentException("src 不能为 null");
    	}
    	String[] splits = src.split(token);
    	return splits[splits.length - 1];
    }
    

	public static String mask(String src, int from, char mask) {
		return mask(src, from, mask, src.length());
	}
	
	
	public static String mask(String src, char mask, int to) {
		return mask(src, 0, mask, to);
	}
	
	
	public static String mask(String src, int from, char mask, int to) {
		if (src == null) {
			return null;
		}
		if (to < from) {
			throw new IllegalArgumentException("'from' must less than 'to'!");
		}
		if (from < 0) {
			throw new IllegalArgumentException("'from' must greater than or equal 'to'!");
		}
		if (to > src.length()) {
			throw new IllegalArgumentException("'to' must less than or equal the length of 'src'!");
		}
		StringBuilder sb = new StringBuilder(src.substring(0, from));
		for (int i = from; i < to; i++) {
			sb.append(mask);
		}
		sb.append(src.substring(to));
		return sb.toString();
	}
    
}
